//
//  XHPictureView.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/6/30.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit
private let pictureViewCellID = "pictureViewCellID"

private let itemMargin:CGFloat = 5
//一个item的宽高
private let cellWH = (SCREENW - 2 * Margin - 2 * itemMargin)/3

class XHPictureView: UICollectionView {
    
    var pic_urls:[XHPictureInfo]?{
        didSet{
            countLable.text = "\(pic_urls?.count ?? 0)"
//            print(countLable.text)
            let size = getPictureViewSizeCount(pic_urls?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in

                make.size.equalTo(size)
            }
           //拿到数据就刷新ui
            reloadData()
            
            
        }
    }
    //根据张数计算配图行高
    /*
    123
    456 
    789
    
    */
    func getPictureViewSizeCount(count:Int) ->CGSize{
        
    //列数
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
        let row = count == 4 ? 2 : ((count - 1) / 3 + 1)
        // 计算配图宽高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*itemMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*itemMargin
        
        return CGSize(width: w, height: h)
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.clearColor()
        //设置数据源
        dataSource = self
        //注册cell
        registerClass(XHPictureCell.self, forCellWithReuseIdentifier: pictureViewCellID)
        //设置layout？？？
        let layout = collectionViewLayout  as!UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
        //添加控件
        addSubview(countLable)


        
        countLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
    
    private lazy var countLable:UILabel = UILabel(textColor: ThemeColor, textFont: 30)
    
}

extension XHPictureView :UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0 
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellID, forIndexPath: indexPath) as! XHPictureCell
        cell.backgroundColor = RandomColor()
        cell.pictInfo = pic_urls![indexPath.item]
        return cell
        
    }
}

class XHPictureCell:UICollectionViewCell{
    
    var pictInfo:XHPictureInfo?{
        didSet{
            backImgView.sd_setImageWithURL(NSURL(string: pictInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        
        contentView.addSubview(backImgView)
        
        //设置约束
        backImgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
    }
    
    private lazy var backImgView:UIImageView = {
      let img = UIImageView(imgName: "timeline_card_top_background")
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        
        return img
    }()
    
}


